package com.laowang.project.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.laowang.common.exception.CustomException;
import com.laowang.framework.security.LoginUser;
import com.laowang.framework.security.service.TokenService;
import com.laowang.framework.web.domain.AjaxResult;
import com.laowang.project.system.domain.MyCyhVip;
import com.laowang.project.system.domain.WxUser;
import com.laowang.project.system.service.MyCyhVipService;
import com.laowang.project.system.service.WxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.laowang.project.system.mapper.MyCyhActivityMapper;
import com.laowang.project.system.domain.MyCyhActivity;
import com.laowang.project.system.service.MyCyhActivityService;

/**
 * 车友活动Service业务层处理
 *
 * @author laowang
 * @date 2023-04-24
 */
@Service
public class MyCyhActivityServiceImpl implements MyCyhActivityService {
    @Autowired
    private MyCyhActivityMapper myCyhActivityMapper;

    @Autowired
    private WxUserService wxUserService;

    @Autowired
    private TokenService tokenService;



    /**
     * 查询车友活动
     *
     * @param id 车友活动ID
     * @return 车友活动
     */
    @Override
    public MyCyhActivity selectMyCyhActivityById(Integer id) {
        return myCyhActivityMapper.selectMyCyhActivityById(id);
    }

    /**
     * 查询车友活动列表
     *
     * @param myCyhActivity 车友活动
     * @return 车友活动
     */
    @Override
    public List<MyCyhActivity> selectMyCyhActivityList(MyCyhActivity myCyhActivity) {
        return myCyhActivityMapper.selectMyCyhActivityList(myCyhActivity);
    }

    /**
     * 新增车友活动
     *
     * @param myCyhActivity 车友活动
     * @return 结果
     */
    @Override
    public int insertMyCyhActivity(MyCyhActivity myCyhActivity) {

        // 不是公益活动
        if (!myCyhActivity.getCate().equals(3)){
            if (myCyhActivity.getCheckUser() == null || myCyhActivity.getCheckUser().equals(0) ){
                throw  new CustomException("该活动未设置核销人");
            }
            WxUser wxUser = wxUserService.selectWxUserById(myCyhActivity.getCheckUser());
            if (wxUser == null){
                throw  new CustomException("该活动核销人不存在");
            }

            Integer checkUser = wxUser.getCheckUser();
            if (!checkUser.equals(1)){
                wxUser.setCheckUser(1);
                wxUserService.updateWxUser(wxUser);

                //刷新
                //LoginUser loginUser = new LoginUser(wxUser, "wxApp");
                //tokenService.refreshToken(loginUser);
            }




        }



        return myCyhActivityMapper.insertMyCyhActivity(myCyhActivity);
    }

    /**
     * 修改车友活动
     *
     * @param myCyhActivity 车友活动
     * @return 结果
     */
    @Override
    public int updateMyCyhActivity(MyCyhActivity myCyhActivity) {
        if (!myCyhActivity.getCate().equals(3)){
            if (myCyhActivity.getCheckUser() == null || myCyhActivity.getCheckUser().equals(0) ){
                throw  new CustomException("该活动未设置核销人");
            }
            WxUser wxUser = wxUserService.selectWxUserById(myCyhActivity.getCheckUser());
            if (wxUser == null){
                throw  new CustomException("该活动核销人不存在");
            }

            Integer checkUser = wxUser.getCheckUser();
            if (!checkUser.equals(1)){
                wxUser.setCheckUser(1);
                wxUserService.updateWxUser(wxUser);

                //刷新
                //LoginUser loginUser = new LoginUser(wxUser, "wxApp");
                //tokenService.refreshToken(loginUser);
            }
        }

        return myCyhActivityMapper.updateMyCyhActivity(myCyhActivity);
    }

    /**
     * 批量删除车友活动
     *
     * @param ids 需要删除的车友活动ID
     * @return 结果
     */
    @Override
    public int deleteMyCyhActivityByIds(Integer[] ids) {
        return myCyhActivityMapper.deleteMyCyhActivityByIds(ids);
    }

    /**
     * 删除车友活动信息
     *
     * @param id 车友活动ID
     * @return 结果
     */
    @Override
    public int deleteMyCyhActivityById(Integer id) {
        return myCyhActivityMapper.deleteMyCyhActivityById(id);
    }

    @Override
    public List<MyCyhActivity> selectByIdList(List<Integer> collect) {
        return myCyhActivityMapper.selectByIdList(collect);
    }

    @Override
    public MyCyhActivity selectMyCyhActivityByIdWeb(Integer id) {
        return myCyhActivityMapper.selectMyCyhActivityByIdWeb(id);
    }

    @Override
    public void insertMyCyhActivityWeb(MyCyhActivity myCyhActivity) {
        insertMyCyhActivity(myCyhActivity);
    }

    @Override
    public List<MyCyhActivity> selectMyCyhActivityListWeb(MyCyhActivity myCyhActivity) {
        return myCyhActivityMapper.selectMyCyhActivityListWeb(myCyhActivity);
    }

    @Override
    public List<MyCyhActivity> selectMyCyhActivitymyList(Integer id) {
        return myCyhActivityMapper.selectMyCyhActivitymyList(id);
    }

    @Override
    public List<Integer> selectActivityIdByCreateBy(Long userId) {
       return   myCyhActivityMapper.selectActivityIdByCreateBy(userId);
    }

    @Override
    public MyCyhActivity selectMyCyhActivityByIdWeb1(Integer id) {
        return myCyhActivityMapper.selectMyCyhActivityByIdWeb1(id);
    }
}
